<template>
  <div>
    <div class="sure_content" v-if="$route.query.ptype == '2'">
      <p class="first_text">手机充值</p>
      <p class="second_text">￥40.00</p>
    </div>
    <div class="sure_content1" v-if="$route.query.ptype == '1'">
      <img src="@/assets/images/citizen/ggoilysf.png" alt />
    </div>
    <div class="hwo_pay1" v-if="$route.query.ptype == '1'">
      <span>金额</span>
      <span>￥40.00</span>
    </div>
    <div class="hwo_pay">支付方式</div>
    <div class="wx_pay">
      <div class="wx_left">
        <img src="@/assets/images/citizen/ysficon.png" alt />
        <span>云闪付</span>
        <span class="payimg"></span>
      </div>
      <div class="wx_right">
        <img src="@/assets/images/citizen/ggradio.png" alt />
      </div>
    </div>
    <div class="pay_money" @click="payNow">立即支付</div>
  </div>
  <!-- coin>=money*100? '0.01' : (money-(coin/100)==0.01? '0.01' : (money-(coin/100) >= 0? money-(coin/100).toFixed(2) : 0.01 -->
</template>
<script>
// import weixin from '@/components/wx/wx.js';
import { Toast, Dialog } from 'vant';
import wx from 'weixin-js-sdk';
export default {
  data() {
    return {
      Toast
    };
  },
  methods: {
    payNow() {
      Dialog.confirm({
        message: `确认支付`,
        confirmButtonText: '立即支付',
        cancelButtonText: '取消'
      })
        .then(() => {
          // 调起支付
          this.$http
            .post('/oneYuanPurchase/submitOrder', {
              type: '2',
              productName: this.$route.query.ptype
                ? this.$route.query.ptype === '1'
                  ? '油'
                  : '话费'
                : '',
              telphone: localStorage.getItem('haveNum'),
              idCardNo: localStorage.getItem('haveId'),
              username: localStorage.getItem('haveName'),
              bankUserId: localStorage.getItem('message'),
              phone: this.$route.query.phone ? this.$route.query.phone : ''
            })
            .then(result => {
              this.id = JSON.stringify(result.data.object);
              if (result.data.code === '0') {
                this.$http
                  .post('oneYuanPurchase/topay', {
                    orderId: this.id,
                    payType: '4',
                    telphone: localStorage.getItem('haveNum'),
                    idCardNo: localStorage.getItem('haveId'),
                    username: localStorage.getItem('haveName'),
                    bankUserId: localStorage.getItem('message')
                  })
                  .then(res => {
                    console.log(res);
                    if (res.data.code === '0') {
                      // document.write(res.data.object.wechatPayURL);
                      window.location.href = res.data.object.wechatPayURL;
                      //   // window.location.href = res.data.object.wechatPayURL;
                      //   wx.ready(() => {
                      //     wx.chooseWXPay({
                      //       timestamp: res.data.object.timeStamp,
                      //       nonceStr: res.data.object.nonceStr,
                      //       package: res.data.object.prepayId,
                      //       signType: 'MD5',
                      //       paySign: res.data.object.sign,
                      //       success: res => {
                      //         window.location.href =
                      //           '/backPay/orderstatus?orderNo=' + this.id;
                      //         // Toast(JSON.stringify(res));
                      //         // Toast('支付成功~');
                      //         // setTimeout(() => {
                      //         // this.$http
                      //         //   .get('/backPay/orderstatus?orderNo=' + this.id)
                      //         //   .then(res => {
                      //         // Toast(JSON.stringify(res));
                      //         // if (res === 'success') {
                      //         //   console.log(res);
                      //         //   this.$router.push({
                      //         //     path:'/paySuccess'
                      //         //   });
                      //         // }
                      //         // });
                      //         // }, 2000);
                      //       },
                      //       // 支付取消回调函数
                      //       cancel: res => {
                      //         Toast('取消支付~');
                      //       },
                      //       // 支付失败回调函数
                      //       fail: res => {
                      //         Toast('支付失败~');
                      //       }
                      //     });
                      //   });
                    } else {
                      Toast(res.data.message);
                    }
                    // console.log(this.id);
                  });
              } else {
                Toast(result.data.message);
              }
            });
        })
        .catch(() => {});
    }
  },
  mounted() {
    this.$http
      .post('/wx/getsignature', {
        url: window.location.href.split('#')[0]
      })
      .then(res => {
        if (res) {
          wx.config({
            debug: false,
            appId: res.data.appId, // 和获取Ticke的必须一样------必填，公众号的唯一标识
            timestamp: res.data.timestamp, // 必填，生成签名的时间戳
            nonceStr: res.data.noncestr, // 必填，生成签名的随机串
            signature: res.data.signature, // 必填，签名
            jsApiList: [
              'onMenuShareTimeline',
              'onMenuShareAppMessage',
              'hideMenuItems',
              'chooseImage',
              'chooseWXPay'
            ]
          });
          // 处理验证失败的信息
          wx.error(function(res) {
            console.log('验证失败返回的信息:', res);
          });
          // 处理验证成功的信息
        } else {
          console.log(res.data.message);
        }
      })
      .catch(err => {
        console.log(err);
      });
    if (this.$route.query.isUseCoin === '') {
      Dialog.alert({
        message: `请求错误`
      }).then(() => {
        this.$router.push({
          path: '/'
        });
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.sure_content {
  width: 750px;
  height: 250px;
  background-color: #ffffff;
  box-shadow: 0px 2px 0px 0px rgba(224, 223, 221, 0.52);
  text-align: center;
  padding-top: 68px;
  .first_text {
    font-size: 30px;
    color: #3b3b3b;
  }
  .second_text {
    font-size: 72px;
    color: #010101;
  }
}
.sure_content1 {
  width: 750px;
  height: 300px;
  background-color: #ffffff;
  box-shadow: 0px 2px 0px 0px rgba(224, 223, 221, 0.52);
  text-align: center;
  align-items: center;
  display: flex;
  align-items: center;
  justify-content: center;
  // padding-top: 68px;
  img {
    width: 679px;
    height: 257px;
  }
}
.hwo_pay {
  width: 750px;
  height: 88px;
  background-color: #ffffff;
  box-shadow: 0px -2px 0px 0px rgba(234, 234, 234, 0.35);
  margin-top: 32px;
  line-height: 88px;
  font-size: 30px;
  color: #454545;
  padding-left: 29px;
}
.hwo_pay1 {
  width: 750px;
  height: 88px;
  background-color: #ffffff;
  box-shadow: 0px -2px 0px 0px rgba(234, 234, 234, 0.35);
  margin-top: 32px;
  line-height: 88px;
  font-size: 30px;
  color: #454545;
  padding-left: 29px;
  padding-right: 29px;
  display: flex;
  justify-content: space-between;
  :nth-child(2) {
    color: red;
  }
}
.wx_pay {
  width: 750px;
  height: 88px;
  background-color: #ffffff;
  margin-top: 2px;
  display: flex;
  justify-content: space-between;
  .wx_left {
    display: flex;
    padding-left: 32px;
    line-height: 88px;
    .payimg {
      width: 190px;
      height: 27px;
      align-self: center;
      margin-left: 20px;
      background-image: url(../../../assets/images/citizen/ggthot.png);
      background-size: 100% 100%;
      background-repeat: no-repeat;
    }
    img {
      display: block;
      width: 40px;
      height: 36px;
      line-height: 88px;
      margin-top: 26px;
      margin-right: 20px;
    }
    span {
      font-size: 30px;
      letter-spacing: -1px;
      color: #1c1c1e;
    }
  }
  .wx_right {
    width: 38px;
    height: 38px;
    margin-top: 26px;
    margin-right: 33px;
  }
}
.pay_money {
  width: 690px;
  height: 88px;
  background-color: #59b7f5;
  box-shadow: 0px -2px 0px 0px #e0dfdd;
  border-radius: 8px;
  text-align: center;
  line-height: 88px;
  color: #fafafb;
  font-size: 32px;
  margin: 82px auto;
}
</style>